package cn.wolfcode.redis.test.SpringBoot;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

@SpringBootTest
public class SpringBootRedis {

    @Autowired
    //private RedisTemplate<String,Object> template;
    //StringRedisTemplate<String,String>
    //约定：key 是字符串，value也是字符串（value值中的泛型是字符串）
    private StringRedisTemplate template;

    @Test
    public void StringTestRedis(){
        template.opsForValue();//操作字符串
        template.opsForHash();//操作hash
        template.opsForList();//操作list
        template.opsForSet();//操作set
        template.opsForZSet();//操作有序set


        //设置
        template.opsForValue().set("name","hongrui");
        template.opsForValue().set("age","18");
        //获取
        System.out.println(template.opsForValue().get("name"));
        //增1
        System.out.println(template.boundValueOps("age").increment(1));
        //减1
        System.out.println(template.boundValueOps("age").increment(-1));
        //删除
        template.delete("age");
        //设置过期时间
        template.opsForValue().set("age","20",5);
        //获取过期时间
        System.out.println(template.getExpire("age"));
        //不存在，操作添加
        template.opsForValue().setIfAbsent("age","25");


    }

    @Test
    public void  HashTestRedis(){
        //设置
        template.opsForHash().put("user","name","hongrui");
        template.opsForHash().put("user","age","19");
        //获取指定值
        System.out.println(template.opsForHash().get("user", "name"));
        //获取所有值
        System.out.println(template.opsForHash().values("user"));
        //判断是否存在
        System.out.println(template.opsForHash().hasKey("user", "name"));
        //删除
        template.opsForHash().delete("user","name");

    }


    @Test
    public void ListTestRedis(){
        //往右添加
        template.opsForList().rightPush("hobby","java");
        template.opsForList().rightPush("hobby","c");
        template.opsForList().rightPush("hobby","c++");
        template.opsForList().rightPush("hobby","python");
        //获取
        System.out.println(template.opsForList().range("hobby", 0, -1));
        //获取长度
        System.out.println(template.opsForList().size("hobby"));
        //弹出最右边
        System.out.println(template.opsForList().rightPop("hobby"));

    }


    @Test
    public void  SetTestRedis(){

        //添加
        template.opsForSet().add("myset","a");
        template.opsForSet().add("myset","b");
        template.opsForSet().add("myset","c");
        template.opsForSet().add("myset","d");
        template.opsForSet().add("myset1","c");
        template.opsForSet().add("myset1","d");
        template.opsForSet().add("myset1","e");
        template.opsForSet().add("myset1","f");
        //列出集合
        template.opsForSet().members("myset").forEach(System.out::println);
        //交集
        System.out.println(template.opsForSet().intersect("myset", "myset1"));
        //并集
        System.out.println(template.opsForSet().union("myset", "myset1"));
        //差集
        template.opsForSet().difference(",myset","myset1");
    }

    @Test
    public void ZsetTestRedis(){
        //添加
        template.opsForZSet().add("players","A",50);
        template.opsForZSet().add("players","B",70);
        template.opsForZSet().add("players","C",80);
        template.opsForZSet().add("players","D",59);
        //升序输出
        System.out.println(template.opsForZSet().reverseRange("players", 0, -1));
        //修改
        System.out.println(template.opsForZSet().incrementScore("players", "A", 55));
        //获取排名
        System.out.println(template.opsForZSet().rank("players", "B"));
        //个数
        template.opsForZSet().zCard("players");
    }

}
